<?php


namespace app\common\model;


use app\user\model\User as UserModel;
use think\Db;
use think\facade\Request;
use think\Model;

class UserIncome extends  Model
{
//    //收益结算
//    public static function ()
//    {
//
//    }

    //获取我的钱包的信息
    public static function wallet($user_id)
    {
        //可提现的金额
        $can_take_out=  Db::table('user')->where([
            'id'=>$user_id
        ])->value('can_take_out') ;

        if (!$can_take_out)$can_take_out=0;

        //已经提现
        $took_out=Db::table('money_take_out')->where([
            'user_id'=>$user_id,
            'status'=>2,            //-1申请被拒绝 1.申请中，2.提现成功
        ])->sum('money');

        //总共收益(激励广告+推广会员的分成)
        $sum_income=   self::where([
            'user_id'=>$user_id
        ])->sum('money');

        //分销带来的收益
        $lv_1_sum = self::where("lv_1_id=$user_id")->field('lv_1_income')->sum('lv_1_income');
        $lv_2_sum = self::where("lv_2_id=$user_id")->field('lv_2_income')->sum('lv_2_income');

        $sum_income= round($sum_income+$lv_1_sum+$lv_2_sum,2) ;

        //冻结的金额
        $no_take_out=  self::where([
            'user_id'=>$user_id,
            'freeze'=>1
        ])->sum('money');

        $no_take_out_lv_1=  self::where([
            'lv_1_id'=>$user_id,
            'freeze'=>1
        ])->sum('lv_1_income');
        $no_take_out_lv_2=  self::where([
            'lv_2_id'=>$user_id,
            'freeze'=>1
        ])->sum('lv_2_income');

        $no_take_out= round($no_take_out+$no_take_out_lv_1+$no_take_out_lv_2 , 2) ;

        return  [
            "can_take_out"=>$can_take_out,
            "took_out"=>$took_out,
            "sum_income"=>$sum_income,
            //冻结的金额
            "no_take_out"=>$no_take_out,
            'lv_1_sum'=>$lv_1_sum,
            'lv_2_sum'=>$lv_2_sum,
            'no_take_out_lv_1'=>$no_take_out_lv_1,
            "no_take_out_lv_2"=>$no_take_out_lv_2
        ];
    }

}
